package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.Admin;
import com.wsd.smartcarwasher.service.AdminService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "管理员相关操作")
@Controller
@CrossOrigin
public class AdminController {
    @Resource
    private AdminService adminService;
    /**
     * 添加管理员
     * @param admin 管理员信息
     * @return 添加结果
     */
    @ApiOperation(value="新建管理员", notes = "账号管理", produces = "application/json", response = ModelMap.class)
    //@ApiImplicitParam(name = "admin", value = "管理员信息", required = true, dataType = "Admin")
    @RequestMapping(value = "/v1/manager", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap addAdmin(@RequestBody @ApiParam Admin admin) {
        if (admin == null || !admin.isFieldsValid()) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
        if (!StringUtil.isValidPhoneNumber(admin.getCellphone())) {
            return ReturnUtil.Error(ResultState.WRONG_PHONE.getMsg());
        }
        if (adminService.hasAdminWithCellphone(admin.getCellphone())) {
            return ReturnUtil.Error(ResultState.PHONE_USED.getMsg());
        }
        int result = adminService.addAdmin(admin);
        if (result == 0) {
            return ReturnUtil.Error(ResultState.ADD_ADMIN_FAIL.getMsg());
        }
        admin.setSalt(null);
        return ReturnUtil.Success(ResultState.ADD_ADMIN_SUCCESS.getMsg(), admin);

    }

    @ApiOperation(value="查询管理员", notes = "账号管理", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "authlevel", value = "管理员信息", required = false, dataType = "Admin")
    @RequestMapping(value = "/v1/managers", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap queryUsersWithRole(@RequestParam(value = "authlevel", required = false) String authlevel) {
        return ReturnUtil.Success(ResultState.QUERY_ADMIN_SUCCESS.getMsg(), adminService.queryAdminWithAuthLevel(authlevel));
    }

    @ApiOperation(value="删除管理员", notes = "账号管理", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "adminId", value = "管理员id", required = true, dataType = "Integer")
    @RequestMapping(value = "/v1/delete_manager", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap deleteAdmin(@RequestParam("id") int adminId) {
        Admin oldAdmin = adminService.getAdminById(adminId);
        if (oldAdmin == null) {
            return ReturnUtil.Error(ResultState.ADMIN_NOT_EXIST.getMsg());
        }
        int changeRow = adminService.deleteAdmin(adminId);
        if (changeRow == 0) {
            return ReturnUtil.Error(ResultState.DELETE_ADMIN_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.DELETE_ADMIN_SUCCESS.getMsg());
    }

    @ApiOperation(value="更新管理员", notes = "账号管理", produces = "application/json", response = ModelMap.class)
    //@ApiImplicitParam(name = "admin", value = "管理员信息", required = true, dataType = "Admin")
    @RequestMapping(value = "/v1/update_manager", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap updateAdmin(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true)Admin admin) {
        if (admin.getId() == null) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
        Admin oldAdmin = adminService.getAdminById(admin.getId());
        if (oldAdmin == null) {
            return ReturnUtil.Error(ResultState.ADMIN_NOT_EXIST.getMsg());
        }
        if(!oldAdmin.getCellphone().equals(admin.getCellphone())){
            if (adminService.hasAdminWithCellphone(admin.getCellphone())) {
                return ReturnUtil.Error(ResultState.PHONE_USED.getMsg());
            }
        }

        admin.setSalt(oldAdmin.getSalt());
        int changeRow = adminService.updateAdmin(admin);
        if (changeRow == 0) {
            return ReturnUtil.Error(ResultState.UPDATE_ADMIN_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.UPDATE_ADMIN_SUCCESS.getMsg());
    }



}
